home *** CD-ROM | disk | FTP | other *** search
/ 3D GFX / 3D GFX.iso / amiutils / i_l / irit5 / cagd_lib / bsp_gen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-31  |  14.1 KB  |  301 lines

  1. /******************************************************************************
  2. * Bsp-Gen.c - Bspline generic routines.                          *
  3. *******************************************************************************
  4. * Written by Gershon Elber, Aug. 90.                          *
  5. ******************************************************************************/
  6.  
  7. #include "cagd_loc.h"
  8.  
  9. /*****************************************************************************
  10. * DESCRIPTION:                                                               M
  11. *   Allocates the memory required for a new Bspline surface.             M
  12. *                                                                            *
  13. * PARAMETERS:                                                                M
  14. *   ULength:      Number of control points in the U direction.               M
  15. *   VLength:      Number of control points in the V direction.               M
  16. *   UOrder:       The order of the surface in the U direction.               M
  17. *   VOrder:       The order of the surface in the V direction.               M
  18. *   PType:        Type of control points (E2, P3, etc.).                     M
  19. *                                                                            *
  20. * RETURN VALUE:                                                              M
  21. *   CagdSrfStruct *:   An uninitialized freeform Bspline surface.            M
  22. *                                                                            *
  23. * KEYWORDS:                                                                  M
  24. *   BspSrfNew, allocation                                                    M
  25. *****************************************************************************/
  26. CagdSrfStruct *BspSrfNew(int ULength,
  27.              int VLength,
  28.              int UOrder,
  29.              int VOrder,
  30.              CagdPointType PType)
  31. {
  32.     CagdSrfStruct *Srf;
  33.  
  34.     if (ULength < UOrder || VLength < VOrder) {
  35.     CAGD_FATAL_ERROR(CAGD_ERR_WRONG_ORDER);
  36.     return NULL;
  37.     }
  38.  
  39.     Srf = CagdSrfNew(CAGD_SBSPLINE_TYPE, PType, ULength, VLength);
  40.  
  41.     Srf -> UKnotVector = (CagdRType *) IritMalloc(sizeof(CagdRType) *
  42.                                (UOrder + ULength));
  43.     Srf -> VKnotVector = (CagdRType *) IritMalloc(sizeof(CagdRType) *
  44.                                (VOrder + VLength));
  45.  
  46.     Srf -> UOrder = UOrder;
  47.     Srf -> VOrder = VOrder;
  48.  
  49.     return Srf;
  50. }
  51.  
  52. /*****************************************************************************
  53. * DESCRIPTION:                                                               M
  54. *   Allocates the memory required for a new, possibly periodic, Bspline      M
  55. * surface.                                     M
  56. *                                                                            *
  57. * PARAMETERS:                                                                M
  58. *   ULength:      Number of control points in the U direction.               M
  59. *   VLength:      Number of control points in the V direction.               M
  60. *   UOrder:       The order of the surface in the U direction.               M
  61. *   VOrder:       The order of the surface in the V direction.               M
  62. *   UPeriodic:    Is this surface periodic in the U direction?               M
  63. *   VPeriodic:    Is this surface periodic in the V direction?               M
  64. *   PType:        Type of control points (E2, P3, etc.).                     M
  65. *                                                                            *
  66. * RETURN VALUE:                                                              M
  67. *   CagdSrfStruct *:   An uninitialized freeform Bspline surface. If both    M
  68. *                      UPeriodic and VPeriodic are FALSE, this function is   M
  69. *                      identical to BspSrfNew.                     M
  70. *                                                                            *
  71. * KEYWORDS:                                                                  M
  72. *   BspPeriodicSrfNew, allocation                                            M
  73. *****************************************************************************/
  74. CagdSrfStruct *BspPeriodicSrfNew(int ULength,
  75.                  int VLength,
  76.                  int UOrder,
  77.                  int VOrder,
  78.                  CagdBType UPeriodic,
  79.                  CagdBType VPeriodic,
  80.                  CagdPointType PType)
  81. {
  82.     CagdSrfStruct *Srf;
  83.  
  84.     if (ULength < UOrder || VLength < VOrder) {
  85.     CAGD_FATAL_ERROR(CAGD_ERR_WRONG_ORDER);
  86.     return NULL;
  87.     }
  88.  
  89.     Srf = CagdPeriodicSrfNew(CAGD_SBSPLINE_TYPE, PType, ULength, VLength,
  90.                  UPeriodic, VPeriodic);
  91.  
  92.     Srf -> UKnotVector = (CagdRType *) IritMalloc(sizeof(CagdRType) *
  93.                (UOrder + ULength + (UPeriodic ? UOrder - 1 : 0)));
  94.     Srf -> VKnotVector = (CagdRType *) IritMalloc(sizeof(CagdRType) *
  95.                (VOrder + VLength + (VPeriodic ? VOrder - 1 : 0)));
  96.  
  97.     Srf -> UOrder = UOrder;
  98.     Srf -> VOrder = VOrder;
  99.  
  100.     return Srf;
  101. }
  102.  
  103. /*****************************************************************************
  104. * DESCRIPTION:                                                               M
  105. *   Allocates the memory required for a new Bspline curve.             M
  106. *                                                                            *
  107. * PARAMETERS:                                                                M
  108. *   Length:     Number of control points                                     M
  109. *   Order:      The order of the curve                                       M
  110. *   PType:      Type of control points (E2, P3, etc.).                       M
  111. *                                                                            *
  112. * RETURN VALUE:                                                              M
  113. *   CagdCrvStruct *:   An uninitialized freeform Bspline curve.              M
  114. *                                                                            *
  115. * KEYWORDS:                                                                  M
  116. *   BspCrvNew, allocation                                                    M
  117. *****************************************************************************/
  118. CagdCrvStruct *BspCrvNew(int Length, int Order, CagdPointType PType)
  119. {
  120.     CagdCrvStruct *Crv;
  121.  
  122.     if (Length < Order) {
  123.     CAGD_FATAL_ERROR(CAGD_ERR_WRONG_ORDER);
  124.     return NULL;
  125.     }
  126.  
  127.     Crv = CagdCrvNew(CAGD_CBSPLINE_TYPE, PType, Length);
  128.  
  129.     Crv -> KnotVector = (CagdRType *) IritMalloc(sizeof(CagdRType) *
  130.                                  (Order + Length));
  131.  
  132.     Crv -> Order = Order;
  133.  
  134.     return Crv;
  135. }
  136.  
  137. /*****************************************************************************
  138. * DESCRIPTION:                                                               M
  139. *   Allocates the memory required for a new, possibly periodic, Bspline      M
  140. * curve.                                     M
  141. *                                                                            *
  142. * PARAMETERS:                                                                M
  143. *   Length:     Number of control points                                     M
  144. *   Order:      The order of the curve                                       M
  145. *   Periodic:   Is this curve periodic?                                      M
  146. *   PType:      Type of control points (E2, P3, etc.).                       M
  147. *                                                                            *
  148. * RETURN VALUE:                                                              M
  149. *   CagdCrvStruct *:   An uninitialized freeform Bspline curve. If Periodic  M
  150. *                      is FALSE, this function is identical to BspCrvNew.    M
  151. *                                                                            *
  152. * KEYWORDS:                                                                  M
  153. *   BspCrvNew, allocation                                                    M
  154. *****************************************************************************/
  155. CagdCrvStruct *BspPeriodicCrvNew(int Length,
  156.                  int Order,
  157.                  CagdBType Periodic,
  158.                  CagdPointType PType)
  159. {
  160.     CagdCrvStruct *Crv;
  161.  
  162.     if (Length < Order) {
  163.     CAGD_FATAL_ERROR(CAGD_ERR_WRONG_ORDER);
  164.     return NULL;
  165.     }
  166.  
  167.     Crv = CagdPeriodicCrvNew(CAGD_CBSPLINE_TYPE, PType, Length, Periodic);
  168.  
  169.     Crv -> KnotVector = (CagdRType *) IritMalloc(sizeof(CagdRType) *
  170.                 (Order + Length + (Periodic ? Order - 1 : 0)));
  171.  
  172.     Crv -> Order = Order;
  173.  
  174.     return Crv;
  175. }
  176.  
  177. /*****************************************************************************
  178. * DESCRIPTION:                                                               M
  179. *   Returns the parametric domain of a Bspline curve.                 M
  180. *                                                                            *
  181. * PARAMETERS:                                                                M
  182. *   Crv:       To get its parametric domain.                                 M
  183. *   TMin:      Where to put the minimal domain's boundary.                   M
  184. *   TMax:      Where to put the maximal domain's boundary.                   M
  185. *                                                                            *
  186. * RETURN VALUE:                                                              M
  187. *   void                                                                     M
  188. *                                                                            *
  189. * KEYWORDS:                                                                  M
  190. *   BspCrvDomain, domain, parametric domain                                  M
  191. *****************************************************************************/
  192. void BspCrvDomain(CagdCrvStruct *Crv, CagdRType *TMin, CagdRType *TMax)
  193. {
  194.     int k = Crv -> Order,
  195.     Len = CAGD_CRV_PT_LST_LEN(Crv);
  196.  
  197.     *TMin = Crv -> KnotVector[k - 1];
  198.     *TMax = Crv -> KnotVector[Len];
  199. }
  200.  
  201. /*****************************************************************************
  202. * DESCRIPTION:                                                               M
  203. *   Returns the parametric domain of a Bspline surface.                 M
  204. *                                                                            *
  205. * PARAMETERS:                                                                M
  206. *   Srf:       To get its parametric domain.                                 M
  207. *   UMin:      Where to put the minimal U domain's boundary.                 M
  208. *   UMax:      Where to put the maximal U domain's boundary.                 M
  209. *   VMin:      Where to put the minimal V domain's boundary.                 M
  210. *   VMax:      Where to put the maximal V domain's boundary.                 M
  211. *                                                                            *
  212. * RETURN VALUE:                                                              M
  213. *   void                                                                     M
  214. *                                                                            *
  215. * KEYWORDS:                                                                  M
  216. *   BspSrfDomain, domain, parametric domain                                  M
  217. *****************************************************************************/
  218. void BspSrfDomain(CagdSrfStruct *Srf,
  219.           CagdRType *UMin,
  220.           CagdRType *UMax,
  221.           CagdRType *VMin,
  222.           CagdRType *VMax)
  223. {
  224.     int UOrder = Srf -> UOrder,
  225.     VOrder = Srf -> VOrder,
  226.     ULen = CAGD_SRF_UPT_LST_LEN(Srf),
  227.     VLen = CAGD_SRF_VPT_LST_LEN(Srf);
  228.  
  229.     *UMin = Srf -> UKnotVector[UOrder - 1];
  230.     *UMax = Srf -> UKnotVector[ULen];
  231.     *VMin = Srf -> VKnotVector[VOrder - 1];
  232.     *VMax = Srf -> VKnotVector[VLen];
  233. }
  234.  
  235. /*****************************************************************************
  236. * DESCRIPTION:                                                               M
  237. *   Returns a curve with open end conditions, similar to given curve.         M
  238. *   Open end curve is computed by extracting a subregion from Crv that is    M
  239. * the entire curve's parametric domain, by inserting multiple knots at the   M
  240. * domain's boundary.                                                         M
  241. *                                                                            *
  242. * PARAMETERS:                                                                M
  243. *   Crv:     To convert to a new curve with open end conditions.             M
  244. *                                                                            *
  245. * RETURN VALUE:                                                              M
  246. *   CagdCrvStruct *: Same curve as Crv but with open end conditions.         M
  247. *                                                                            *
  248. * KEYWORDS:                                                                  M
  249. *   BspCrvOpenEnd, open end conditions                                       M
  250. *****************************************************************************/
  251. CagdCrvStruct *BspCrvOpenEnd(CagdCrvStruct *Crv)
  252. {
  253.     int k = Crv -> Order,
  254.     Len = Crv -> Length;
  255.     CagdCrvStruct
  256.     *OpenCrv = CagdCrvRegionFromCrv(Crv,
  257.                     Crv -> KnotVector[k - 1],
  258.                     Crv -> KnotVector[Len]);
  259.  
  260.     return OpenCrv;
  261. }
  262.  
  263. /*****************************************************************************
  264. * DESCRIPTION:                                                               M
  265. *   Returns a surface with open end conditions, similar to given surface.    M
  266. *   Open end surface is computed by extracting a subregion from Srf that is  M
  267. * the entire surface's parametric domain, by inserting multiple knots at the M
  268. * domain's boundary.                                                         M
  269. *                                                                            *
  270. * PARAMETERS:                                                                M
  271. *   Srf:     To convert to a new curve with open end conditions.             M
  272. *                                                                            *
  273. * RETURN VALUE:                                                              M
  274. *   CagdSrfStruct *: Same surface as Srf but with open end conditions.       M
  275. *                                                                            *
  276. * KEYWORDS:                                                                  M
  277. *   BspSrfOpenEnd, open end conditions                                       M
  278. *****************************************************************************/
  279. CagdSrfStruct *BspSrfOpenEnd(CagdSrfStruct *Srf)
  280. {
  281.     int Uk = Srf -> UOrder,
  282.     ULen = Srf -> ULength,
  283.     Vk = Srf -> VOrder,
  284.     VLen = Srf -> VLength;
  285.     CagdSrfStruct
  286.     *TSrf =
  287.         CagdSrfRegionFromSrf(Srf,
  288.                  Srf -> UKnotVector[Uk - 1],
  289.                  Srf -> UKnotVector[ULen],
  290.                  CAGD_CONST_U_DIR),
  291.     *OpenSrf =
  292.         CagdSrfRegionFromSrf(TSrf,
  293.                  TSrf -> VKnotVector[Vk - 1],
  294.                  TSrf -> VKnotVector[VLen],
  295.                  CAGD_CONST_V_DIR);
  296.  
  297.     CagdSrfFree(TSrf);
  298.  
  299.     return OpenSrf;
  300. }
  301.